-- fortune.lua
-- Adds a fortune to your message
-- Version 1.0
-- By Tyrone

---------------------------------------

--[[
Changelog:
1.0
-#fortune anywhere in message to replace with a random fortune + NewLine

]]



-- set up all command prefixes


COOLDOWN = 60 -- plugin cooldown, 0 for none
_,bot_name = VH:GetConfig("config", "hub_security")
bot_desc = "bot description here"
bot_email = "bot@nowhere.com"
use_bot = false

-- insert other functions here
fortunes = {"Outlook good", "Good news will come to you by mail", "Reply, hazy, try again", "Bad Luck",
			"Very Bad Luck", "Better not tell you now", "You will meet a dark handsome stranger", "Good Luck",
			"Average Luck", "Excellent Luck", "Godly Luck"}

---------------------------------------
-- you can ignore below here

function Main()
    -- Check if script should add a new bot.
    if (use_bot) then
    -- Add new bot.
        VH:AddRobot(bot_name, 10, bot_desc, "Bot ", bot_email, "0")
    end
end

function UnLoad()
	if (use_bot) then
    	VH:DelRobot(bot_name)
	end
end

function VH_OnParsedMsgChat(nick, data)
	if (string.find(data, "^%.")) then
		return 1
    else
		position = string.find(data, "#fortune")
		if position then
			if (position == 1) and (string.len(data) == 8) then
				message = string.gsub(data, "#fortune", "Your fortune: "..fortunes[math.random(1, table.getn(fortunes))])
			elseif (position == 1) then
				message = string.gsub(data, "#fortune", "Your fortune: "..fortunes[math.random(1, table.getn(fortunes))]..string.format("\n"))
			else
				message = string.gsub(data, "#fortune", string.format("\n").."Your fortune: "..fortunes[math.random(1, table.getn(fortunes))]..string.format("\n"))
			end
			SendMessageToHub(message, nick)
			return 0
		else
			return 1
		end
	end
end

function ready(nick)
    if (timeLeft[nick] == 0) then
		timeLeft[nick] = COOLDOWN
	elseif (timeLeft[nick] == nil) then
		timeLeft[nick] = COOLDOWN
	else
		SendMessageToUser("This command can only be used every "..COOLDOWN.." seconds.  You have "..timeLeft[nick].." seconds to wait.", nick, bot_name)
		return true
	end
	return false
end

function SendMessageToHub(data, from)
	return VH:SendDataToAll("<"..from.."> "..data.."|",0,10)
end

function SendMessageToUser(data, nick, from)
	return VH:SendDataToUser("<"..from.."> "..data.."|", nick)
end

function SendPrivateMessageToUser(user, data, from)
    return VH:SendDataToUser("$To: "..user.." From: "..from.." $<"..from.."> "..data.."|", user);
end

function DisplayHelpMessage(cmd, msg, nick)
    SendMessageToUser("        ."..cmdNames[1].." "..cmd, nick, bot_name)
    SendMessageToUser("        "..msg, nick, bot_name)
end

function UserIsOnline(nick)
	result, sIP = VH:GetUserIP(nick)
	if (string.len(sIP) < 7) then
		return false
	else
		return true
	end
end

function UserIP(nick)
	result, sIP = VH:GetUserIP(nick)
	if (string.len(sIP) < 7) then
		return 0
	else
		return sIP
	end
end

function UserClass(nick)
	res, class = VH:GetUserClass(nick)
	if res and class then return class else return 0 end
end

function SendEmoteToHub(data, from)
	result, err = VH:SendDataToAll("<"..from.."> ".."/me "..data.."|",0,10)
	return 1
end
